package com.example.weakreference;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.weakreference.bean.IActivity;

public class ActivityB extends AppCompatActivity implements IActivity {

    MyRunnable myRunnable;
    R          mR;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.example.weakreference.R.layout.activity_b);

        //        myRunnable = new MyRunnable(this);
        //        new Thread(myRunnable).start();

        Thread th = new Thread(mR = new R(this));
        th.start();

        //        new Thread(() -> {
        //            try {
        //                Thread.sleep(1000);
        //            } catch (InterruptedException e) {
        //                e.printStackTrace();
        //            }
        //            th.interrupt();
        //        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        System.out.println("onDestroy()");
        if (myRunnable != null) {
            myRunnable.reference.clear();
            //            myRunnable.clear();
            //            myRunnable = null;
        }

        if (mR != null) {
            mR.iActivity = null;
            System.out.println(mR.iActivity);
        }
    }

    private static class R implements Runnable {
        IActivity iActivity;

        public R(IActivity iActivity) {
            this.iActivity = iActivity;
        }


        @Override
        public void run() {
            System.out.println("Thread run context=" + iActivity);
            // 死循环
            boolean b = true;
            while (b) {}
            System.out.println("sleep()完了 ");
        }
    }
}
